package com.os.opencv.java.chapter11;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.SIFT;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;

public class BruteForceMatch {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取图像并在屏幕上显示
        Mat img1 = Imgcodecs.imread("pics/face.jpg");
        HighGui.imshow("img1", img1);
        HighGui.waitKey(0);

        //用sift算法检测图像1的关键点
        SIFT sift = SIFT.create(50);
        MatOfKeyPoint kp1 = new MatOfKeyPoint();
        Mat descriptor1 = new Mat();
        sift.detect(img1, kp1);

        //计算图像1中的关键描述子
        sift.compute(img1, kp1, descriptor1);

        //读取图像2并在屏幕上显示
        Mat img2 = Imgcodecs.imread("pics/face.jpg");
        HighGui.imshow("img2", img2);
        HighGui.waitKey(0);

        //用sift算法检测图像2的关键点
        MatOfKeyPoint kp2 = new MatOfKeyPoint();
        Mat descriptor2 = new Mat();
        sift.detect(img2, kp2);

        //计算图像2中关键点的描述子
        sift.compute(img2, kp2, descriptor2);

        //创建一个descriptormatcher对象
        DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
        MatOfDMatch matches = new MatOfDMatch();

        //进行关键点匹配
        matcher.match(descriptor1, descriptor2, matches);

        //在dst上画出匹配的关键点
        Mat dst = new Mat();
        Features2d.drawMatches(img1, kp1, img2, kp2, matches, dst);

        //在屏幕上显示匹配结果
        HighGui.imshow("matches", dst);
        HighGui.waitKey(0);

        System.exit(0);
    }
}
